package io.unicorn.modules.water.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import io.unicorn.common.utils.ExcelUtils;
import io.unicorn.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.unicorn.modules.water.entity.TDeviceInfoEntity;
import io.unicorn.modules.water.service.TDeviceInfoService;
import io.unicorn.common.utils.PageUtils;
import io.unicorn.common.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * ${comments}
 *
 * @author LNCG
 * @email LNCG@USTL.EDU.CN
 * @date 2021-05-03 08:18:01
 */
@RestController
@RequestMapping("water/tdeviceinfo")
public class TDeviceInfoController {
    @Autowired
    private TDeviceInfoService tDeviceInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("water:tdeviceinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tDeviceInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{deviceId}")
    @RequiresPermissions("water:tdeviceinfo:info")
    public R info(@PathVariable("deviceId") String deviceId){
        TDeviceInfoEntity tDeviceInfo = tDeviceInfoService.getById(deviceId);

        return R.ok().put("tDeviceInfo", tDeviceInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("water:tdeviceinfo:save")
    public R save(@RequestBody TDeviceInfoEntity tDeviceInfo){
        tDeviceInfoService.save(tDeviceInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("water:tdeviceinfo:update")
    public R update(@RequestBody TDeviceInfoEntity tDeviceInfo){
        ValidatorUtils.validateEntity(tDeviceInfo);
        tDeviceInfoService.updateById(tDeviceInfo);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("water:tdeviceinfo:delete")
    public R delete(@RequestBody String[] deviceIds){
        tDeviceInfoService.removeByIds(Arrays.asList(deviceIds));

        return R.ok();
    }

    @RequestMapping("/export")
    @RequiresPermissions("water:tclientinfo:list")
    public void export(@RequestParam Map<String, Object> params,
                       HttpServletRequest request,
                       HttpServletResponse repsonse){
        JSONObject jsonObject = JSONObject.parseObject((String)params.get("params"));
        PageUtils page = tDeviceInfoService.queryPage(jsonObject);
        List<?> listEntity = page.getList();

        ExcelUtils.exportXlsx(
                repsonse,
                "export",
                "io.unicorn.modules.water.entity.TDeviceInfoEntity",
                listEntity);
    }

}
